package com.dmgis.qxfw.dataservice.businessservice.yyhb.service.Impl;

import com.dmgis.qxfw.dataservice.businessservice.api.domain.DbConfig;
import com.dmgis.qxfw.dataservice.businessservice.api.domain.RequestDataBySelect;
import com.dmgis.qxfw.dataservice.businessservice.api.entity.qx.hazevisibility_forecast;
import com.dmgis.qxfw.dataservice.businessservice.api.entity.qx.heavy_forecast;
import com.dmgis.qxfw.dataservice.businessservice.api.entity.qx.township_station;
import com.dmgis.qxfw.dataservice.businessservice.api.entity.xt.sys_config;
import com.dmgis.qxfw.dataservice.businessservice.api.entity.zy.*;
import com.dmgis.qxfw.dataservice.businessservice.common.pojo.*;
import com.dmgis.qxfw.dataservice.businessservice.common.util.DateTime;
import com.dmgis.qxfw.dataservice.businessservice.common.util.JsonUtil;
import com.dmgis.qxfw.dataservice.businessservice.common.util.StringUtil;
import com.dmgis.qxfw.dataservice.businessservice.controller.ProcessRequestController;
import com.dmgis.qxfw.dataservice.businessservice.yyhb.service.EnvirForecastServiceI;
import com.fasterxml.jackson.core.type.TypeReference;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class EnvirForecastServiceImpl implements EnvirForecastServiceI {

    ProcessRequestController procon=new ProcessRequestController();



    @Override
    public List<AirPolConForecast> getEnvirForecastDataGrid(String date1, String date2) {
        List<airpol_con_fcst> listData = new ArrayList<airpol_con_fcst>();
        List<AirPolConForecast> resultList = new ArrayList<AirPolConForecast>();
        Map<String, String> selectParam = new HashMap<String, String>();
        RequestDataBySelect selectRequest = new RequestDataBySelect();
        List<String> columns = new ArrayList<String>();
        columns.add("to_char(observtime, 'yyyy-MM-dd HH24:MI:SS') as observtime");
        columns.add("level");
        columns.add("totalweight");
        columns.add("asseaa");
        columns.add("describe");
        columns.add("condition");
        selectRequest.setColumns(columns.toArray(new String[columns.size()]));
        // 拼接where字段
        StringBuilder strWhere = new StringBuilder();
        int whereCount = 0;
        if (date1 != null) {
            strWhere.append(String.format(" and observtime>='%s'", date1));
        }
        if (date2 != null) {
            strWhere.append(String.format(" and observtime<='%s'", date2));
        }
        if (whereCount == 0 && !StringUtil.IsNullOrEmpty(strWhere.toString())) {
            strWhere = strWhere.replace(0, 4, "");
        }
        selectRequest.setParam(strWhere.toString());
        try {
            selectParam.put("method", "airpol_con_fcst");
            selectParam.put("sqlType", "select");
            selectParam.put("advCode", DbConfig.advCode);
            selectParam.put("param", JsonUtil.object2Json(selectRequest));
            String message =procon.processRequest(selectParam);
            listData = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<airpol_con_fcst>>() {
            });
        } catch (Exception e) {
            // TODO: handle exception
        }
        if (listData != null && listData.size() > 0) {
            for (airpol_con_fcst airpol : listData) {
                AirPolConForecast airpolconforecast = new AirPolConForecast();
                Date date = airpol.getObservtime();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                airpolconforecast.setObservTime(sdf.format(date));
                airpolconforecast.setLevel(airpol.getLevel());
                airpolconforecast.setTotalWeight(airpol.getTotalweight());
                airpolconforecast.setAsseaa(airpol.getAsseaa());
                airpolconforecast.setCondition(airpol.getCondition());
                airpolconforecast.setDescribe(airpol.getDescribe());
                resultList.add(airpolconforecast);
            }
        }
        return resultList;
    }

    @Override
    public List<AirPolRegForecast> getAirPolRegForecast(String s1, String s2) {
        List<airpol_reg_fcst> listData = new ArrayList<airpol_reg_fcst>();
        List<AirPolRegForecast> resultList = new ArrayList<AirPolRegForecast>();
        Map<String, String> selectParam = new HashMap<String, String>();
        RequestDataBySelect selectRequest = new RequestDataBySelect();
        List<String> columns = new ArrayList<String>();
        columns.add(
                "to_char(cast(observtime as timestamp)+ cast((forecasttype)||'hour' as INTERVAL),'yyyy-MM-dd HH24:MI:SS') as observtime");
        columns.add("aqi");
        columns.add("forecasttype");
        if (s1.contains("AQI")) {
            columns.add("aqi");
        }
        if (s1.contains("PM2p5")) {
            columns.add("pm2p5");
        }
        if (s1.contains("PM10")) {
            columns.add("pm10");
        }
        if (s1.contains("O3_1H")) {
            columns.add("o3_1h");
        }
        if (s1.contains("O3_8H")) {
            columns.add("o3_8h");
        }
        selectRequest.setColumns(columns.toArray(new String[columns.size()]));
        // 拼接where字段
        StringBuilder strWhere = new StringBuilder();
        int whereCount = 0;
        if (s2 != null) {
            strWhere.append(String.format(" and observtime='%s'", s2));
        }
        if (whereCount == 0 && !StringUtil.IsNullOrEmpty(strWhere.toString())) {
            strWhere = strWhere.replace(0, 4, "");
        }
        selectRequest.setParam(strWhere.toString());
        try {
            selectParam.put("method", "airpol_reg_fcst");
            selectParam.put("sqlType", "select");
            selectParam.put("advCode", DbConfig.advCode);
            selectParam.put("param", JsonUtil.object2Json(selectRequest));
            String message = procon.processRequest(selectParam);
            listData = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<airpol_reg_fcst>>() {
            });
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println("error!!!!");
        }
        if (listData != null && listData.size() > 0) {
            for (airpol_reg_fcst airpol : listData) {
                AirPolRegForecast airpolregforecast = new AirPolRegForecast();
                Date date = airpol.getObservtime();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                airpolregforecast.setObservTime(sdf.format(date));
                airpolregforecast.setAQI(airpol.getAqi());
                airpolregforecast.setForecastType(airpol.getForecasttype());
                if (s1.contains("AQI")) {
                    airpolregforecast.setAQI(airpol.getAqi());
                }
                if (s1.contains("PM2p5")) {
                    airpolregforecast.setPM2p5(airpol.getPm2p5());
                }
                if (s1.contains("PM10")) {
                    airpolregforecast.setPM10(airpol.getPm10());
                }
                if (s1.contains("O3_1H")) {
                    airpolregforecast.setO3_1H(airpol.getO3_1h());
                }
                if (s1.contains("O3_8H")) {
                    airpolregforecast.setO3_8H(airpol.getO3_8h());
                }
                resultList.add(airpolregforecast);
            }
        }
        return resultList;
    }

    @Override
    public List<AllCityInfo> getAllCityInfo(String s1, String s2, String s3, String s4) {
        // 空气质量回归预报检验
        // 观测值
        List<sum_atmospheric_day_data> listData = new ArrayList<sum_atmospheric_day_data>();
        List<AllCityInfo> resultList = new ArrayList<AllCityInfo>();
        Map<String, String> selectParam = new HashMap<String, String>();
        RequestDataBySelect selectRequest = new RequestDataBySelect();
        List<String> columns = new ArrayList<String>();
        if (s1.contains("AQI")) {
            columns.add("aqi");
        }
        if (s1.contains("PM2p5")) {
            columns.add("concenpm2p5");
        }
        if (s1.contains("PM10")) {
            columns.add("concenpm10");
        }
        if (s1.equals("ConcenO3")) {
            columns.add("conceno3");
        }
        if (s1.contains("O38H")) {
            columns.add("conceno38h");
        }
        columns.add("to_char(observtime, 'yyyy-MM-dd HH24:MI:SS') as observtime");
        columns.add("stationid");
        // 拼接where字段
        StringBuilder strWhere = new StringBuilder();
        int whereCount = 0;
        if (s2 != null) {
            strWhere.append(String.format(" and observtime>='%s'", s2));
        }
        if (s3 != null) {
            strWhere.append(String.format(" and observtime<='%s'", s3));
        }
        if (s4 != null) {
            strWhere.append(String.format(" and stationid='%s'", s4));
        }
        if (whereCount == 0 && !StringUtil.IsNullOrEmpty(strWhere.toString())) {
            strWhere = strWhere.replace(0, 4, "");
        }
        selectRequest.setParam(strWhere.toString());
        selectRequest.setColumns(columns.toArray(new String[columns.size()]));
        selectRequest.setSort(new String[]{"observtime"});
        try {
            selectParam.put("method", "sum_atmospheric_day_data");
            selectParam.put("sqlType", "select");
            selectParam.put("advCode", DbConfig.advCode);
            selectParam.put("param", JsonUtil.object2Json(selectRequest));
            String message = procon.processRequest(selectParam);
            listData = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<sum_atmospheric_day_data>>() {
            });
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println("error!");
        }
        if (listData != null && listData.size() > 0) {
            for (sum_atmospheric_day_data air : listData) {
                AllCityInfo cityinfo = new AllCityInfo();
                if (s1.contains("AQI")) {
                    cityinfo.setAQI(air.getAqi());
                }
                if (s1.contains("PM2p5")) {
                    cityinfo.setConcenPM2p5(air.getConcenpm2p5());
                }
                if (s1.contains("PM10")) {
                    cityinfo.setConcenPM10(air.getConcenpm10());
                }
                if (s1.equals("ConcenO3")) {
                    cityinfo.setConcenO3(air.getConceno3());
                }
                if (s1.contains("O38H")) {
                    cityinfo.setConcenO38H(air.getConceno38h());
                }
                cityinfo.setStationID(air.getStationid());
                cityinfo.setObservTime(air.getObservtime());
                resultList.add(cityinfo);
            }
        }
        return resultList;
    }

    @Override
    public List<AirPolRegForecast> getAgoAirPolRegForecast(String s1, String s2, String s3) {
        // 历史预报值
        List<airpol_reg_fcst> listData = new ArrayList<airpol_reg_fcst>();
        List<AirPolRegForecast> resultList = new ArrayList<AirPolRegForecast>();
        Map<String, String> selectParam = new HashMap<String, String>();
        RequestDataBySelect selectRequest = new RequestDataBySelect();
        List<String> columns = new ArrayList<String>();
        if (s1.contains("AQI")) {
            columns.add("aqi");
        }
        if (s1.contains("ConcenPM2p5")) {
            columns.add("pm2p5");
        }
        if (s1.contains("ConcenPM10")) {
            columns.add("pm10");
        }
        if (s1.equals("ConcenO3")) {
            columns.add("o3_1h");
        }
        if (s1.contains("ConcenO38H")) {
            columns.add("o3_8h");
        }
        columns.add(
                "to_char(cast(observtime as timestamp)+ cast((forecasttype)||'hour' as INTERVAL),'yyyy-MM-dd HH24:MI:SS') as observtime");
        // 拼接where字段
        StringBuilder strWhere = new StringBuilder();
        int whereCount = 0;
        if (s2 != null) {
            strWhere.append(String.format(" and observtime>='%s'", s2));
        }
        if (s3 != null) {
            strWhere.append(String.format(" and observtime<='%s'", s3));
        }
        strWhere.append(String.format(" and forecasttype='%s'", "24"));
        if (whereCount == 0 && !StringUtil.IsNullOrEmpty(strWhere.toString())) {
            strWhere = strWhere.replace(0, 4, "");
        }
        selectRequest.setParam(strWhere.toString());
        selectRequest.setColumns(columns.toArray(new String[columns.size()]));
        selectRequest.setSort(new String[]{"observtime"});
        try {
            selectParam.put("method", "airpol_reg_fcst");
            selectParam.put("sqlType", "select");
            selectParam.put("advCode", DbConfig.advCode);
            selectParam.put("param", JsonUtil.object2Json(selectRequest));
            String message =procon.processRequest(selectParam);
            listData = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<airpol_reg_fcst>>() {
            });
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println("error!");
        }
        if (listData != null && listData.size() > 0) {
            for (airpol_reg_fcst airpol : listData) {
                AirPolRegForecast cityinfo = new AirPolRegForecast();
                if (s1.contains("AQI")) {
                    cityinfo.setAQI(airpol.getAqi());
                }
                if (s1.contains("PM2p5")) {
                    cityinfo.setPM2p5(airpol.getPm2p5());
                }
                if (s1.contains("PM10")) {
                    cityinfo.setPM10(airpol.getPm10());
                }
                if (s1.equals("ConcenO3")) {
                    cityinfo.setO3_1H(airpol.getO3_1h());
                }
                if (s1.contains("O38H")) {
                    cityinfo.setO3_8H(airpol.getO3_8h());
                }
                cityinfo.setObservTime(airpol.getObservtime().toString());
                resultList.add(cityinfo);
            }
        }
        return resultList;
    }

    @Override
    public List<AirPolForecast> getAirPolForecast(String s1, String s2, String s3) throws ParseException {
        List<airpol_fcst> listData = new ArrayList<airpol_fcst>();
        List<AirPolForecast> resultList = new ArrayList<AirPolForecast>();
        Map<String, String> selectParam = new HashMap<String, String>();
        RequestDataBySelect selectRequest = new RequestDataBySelect();
        List<String> columns = new ArrayList<String>();
        columns.add("stationid");
        columns.add("to_char(datechar, 'yyyy-MM-dd HH24:MI:SS') as datechar");
        columns.add("ntimes");
        columns.add("forecasttype");
        columns.add("stationname");
        columns.add("longitude");
        columns.add("latitude");
        columns.add("pm10");
        columns.add("o3_1h");
        columns.add("o3_8h");
        columns.add("pm2_5");
        columns.add("so2");
        columns.add("no2");
        columns.add("co");
        columns.add("aqi");
        columns.add("pri");
        columns.add("grade");
        // 拼接where字段
        StringBuilder strWhere = new StringBuilder();
        int whereCount = 0;
        if (s1 != null) {
            SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date date = sDateFormat.parse(s1);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.DATE, 7);
            strWhere.append(String.format(" and datechar>='%s'", s1));
            strWhere.append(String.format(" and datechar<='%s'", s2));
        }
        strWhere.append(String.format(" and forecasttype='%s'", s3));
        if (s2 != null) {

        }
        if (whereCount == 0 && !StringUtil.IsNullOrEmpty(strWhere.toString())) {
            strWhere = strWhere.replace(0, 4, "");
        }
        selectRequest.setSort(new String[]{"datechar,ntimes"});
        selectRequest.setParam(strWhere.toString());
        selectRequest.setColumns(columns.toArray(new String[columns.size()]));
        try {
            selectParam.put("method", "airpol_fcst");
            selectParam.put("advCode", DbConfig.advCode);
            selectParam.put("sqlType", "select");
            selectParam.put("param", JsonUtil.object2Json(selectRequest));
            String message = procon.processRequest(selectParam);
            listData = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<airpol_fcst>>() {
            });
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println("error!");
        }
        if (listData != null && listData.size() > 0) {
            for (airpol_fcst air : listData) {
                AirPolForecast cityinfo = new AirPolForecast();
                cityinfo.setStationID(air.getStationid());
                cityinfo.setDateChar(air.getDatechar());
                cityinfo.setNtimes(air.getNtimes());
                cityinfo.setStationName(air.getStationname());
                cityinfo.setLongitude(air.getLongitude());
                cityinfo.setLatitude(air.getLatitude());
                cityinfo.setPM2_5(air.getPm25());
                cityinfo.setPM10(air.getPm10());
                cityinfo.setSO2(air.getSo2());
                cityinfo.setNO2(air.getNo2());
                cityinfo.setCO(air.getCo());
                cityinfo.setAQI(air.getAqi());
                cityinfo.setPRI(air.getPri());
                cityinfo.setGRADE(air.getGrade());
                cityinfo.setType(air.getForecasttype()+"");
                resultList.add(cityinfo);
            }
        }
        return resultList;
    }


    @Override
    public List<AirPolForecast> getHourAirPolForecast(String s1, int i, String s2) {
        List<airpol_fcst> listData = new ArrayList<airpol_fcst>();
        List<AirPolForecast> resultList = new ArrayList<AirPolForecast>();
        Map<String, String> selectParam = new HashMap<String, String>();
        RequestDataBySelect selectRequest = new RequestDataBySelect();
        List<String> columns = new ArrayList<String>();
        columns.add("stationid");
        columns.add(
                "to_char(cast(datechar as timestamp)+ cast((ntimes)||'hour' as INTERVAL),'yyyy-MM-dd HH24:MI:SS') as datechar");
        columns.add("stationname");
        columns.add("longitude");
        columns.add("latitude");
        columns.add("pm10");
        columns.add("o3_1h");
        columns.add("o3_8h");
        columns.add("pm2_5");
        columns.add("so2");
        columns.add("no2");
        columns.add("co");
        columns.add("aqi");
        columns.add("pri");
        columns.add("grade");
        // 拼接where字段
        StringBuilder strWhere = new StringBuilder();
        int whereCount = 0;
        if (s1 != null) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            try {
                DateTime s11 = new DateTime(sdf.parse(s1));
                String s12 = s11.AddDays(1).toDateString();
                strWhere.append(String.format(" and datechar>='%s'", s1));
                strWhere.append(String.format(" and datechar<='%s'", s12));
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if (s2 != null) {
            strWhere.append(String.format(" and forecasttype<='%s'", s2));
        }
        strWhere.append(String.format(" and ntimes='%s'", i));
        if (whereCount == 0 && !StringUtil.IsNullOrEmpty(strWhere.toString())) {
            strWhere = strWhere.replace(0, 4, "");
        }
        selectRequest.setParam(strWhere.toString());
        selectRequest.setColumns(columns.toArray(new String[columns.size()]));
        try {
            selectParam.put("method", "airpol_fcst");
            selectParam.put("sqlType", "select");
            selectParam.put("advCode", DbConfig.advCode);
            selectParam.put("param", JsonUtil.object2Json(selectRequest));
            String message =procon.processRequest(selectParam);
            listData = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<airpol_fcst>>() {
            });
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println("error!");
        }
        if (listData != null && listData.size() > 0) {
            for (airpol_fcst air : listData) {
                AirPolForecast cityinfo = new AirPolForecast();
                cityinfo.setStationID(air.getStationid());
                cityinfo.setDateChar(air.getDatechar());
                cityinfo.setStationName(air.getStationname());
                cityinfo.setLongitude(air.getLongitude());
                cityinfo.setLatitude(air.getLatitude());
                cityinfo.setPM2_5(air.getPm25());
                cityinfo.setPM10(air.getPm10());
                cityinfo.setSO2(air.getSo2());
                cityinfo.setNO2(air.getNo2());
                cityinfo.setCO(air.getCo());
                cityinfo.setAQI(air.getAqi());
                cityinfo.setPRI(air.getPri());
                cityinfo.setGRADE(air.getGrade());
                resultList.add(cityinfo);
            }
        }
        return resultList;
    }

    @Override
    public List<VisibilityForecast> getVisibilityForecast(String s1, String s2, String  s3) {
        List<hazevisibility_forecast> listData = new ArrayList<hazevisibility_forecast>();
        List<VisibilityForecast> resultList = new ArrayList<VisibilityForecast>();
        Map<String, String> selectParam = new HashMap<String, String>();
        RequestDataBySelect selectRequest = new RequestDataBySelect();
        List<String> columns = new ArrayList<String>();
        columns.add("to_char(cast(datechar as timestamp)+ cast((ntimes)||'hour' as INTERVAL),'yyyy-MM-dd HH24:MI:SS') as datechar");
        columns.add("hazevisibility");

        // 拼接where字段
        StringBuilder strWhere = new StringBuilder();
        int whereCount = 0;
        if (s1 != null) {
            strWhere.append(String.format(" and stationid ='%s'", s1));
        }
        if (s2 != null) {
            strWhere.append(String.format(" and datechar>='%s'", s2));
            strWhere.append(String.format(" and datechar<='%s'", s3));
        }
        if (whereCount == 0 && !StringUtil.IsNullOrEmpty(strWhere.toString())) {
            strWhere = strWhere.replace(0, 4, "");
        }
        selectRequest.setParam(strWhere.toString());
        selectRequest.setColumns(columns.toArray(new String[columns.size()]));
        try {
            selectParam.put("method", "hazevisibility_forecast");
            selectParam.put("advCode", DbConfig.advCode);
            selectParam.put("sqlType", "select");
            selectParam.put("param", JsonUtil.object2Json(selectRequest));
            String message = procon.processRequest(selectParam);
            listData = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<hazevisibility_forecast>>() {
            });
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println("error!");
        }
        if (listData != null && listData.size() > 0) {
            for (hazevisibility_forecast air : listData) {
                VisibilityForecast cityinfo = new VisibilityForecast();
                cityinfo.setDateChar(air.getDatechar());
                cityinfo.setHazeVisibility(air.getHazevisibility().floatValue());
                resultList.add(cityinfo);
            }
        }
        return resultList;
    }

    @Override
    public List<VisibilityForecast> getOneDayVisibilityForecast(String s1, String s2, String s3) {
        List<hazevisibility_forecast> listData = new ArrayList<hazevisibility_forecast>();
        List<VisibilityForecast> resultList = new ArrayList<VisibilityForecast>();
        Map<String, String> selectParam = new HashMap<String, String>();
        RequestDataBySelect selectRequest = new RequestDataBySelect();
        List<String> columns = new ArrayList<String>();
        if ("时".equals(s3)) {
            columns.add(
                    "to_char(cast(datechar as timestamp)+ cast((ntimes)||'hour' as INTERVAL),'yyyy-MM-dd HH24:MI:SS') as datechar");
            columns.add("hazevisibility");
        } else if ("日".equals(s3)) {
            columns.add("datechar");
            columns.add("avg(hazevisibility) as hazevisibility");
        }
        // 拼接where字段
        StringBuilder strWhere = new StringBuilder();
        int whereCount = 0;
        if (s1 != null) {
            strWhere.append(String.format(" and stationid ='%s'", s1));
        }
        if (s2 != null) {
            strWhere.append(String.format(" and datechar='%s'", s2));
        }
        if (whereCount == 0 && !StringUtil.IsNullOrEmpty(strWhere.toString())) {
            strWhere = strWhere.replace(0, 4, "");
        }
        selectRequest.setParam(strWhere.toString());
        selectRequest.setColumns(columns.toArray(new String[columns.size()]));
        try {
            selectParam.put("method", "hazevisibility_forecast");
            selectParam.put("sqlType", "select");
            selectParam.put("advCode", DbConfig.advCode);
            selectParam.put("param", JsonUtil.object2Json(selectRequest));
            String message =procon.processRequest(selectParam);
            listData = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<hazevisibility_forecast>>() {
            });
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println("error!");
        }
        if (listData != null && listData.size() > 0) {
            for (hazevisibility_forecast air : listData) {
                VisibilityForecast cityinfo = new VisibilityForecast();
                cityinfo.setDateChar(air.getDatechar());
                cityinfo.setHazeVisibility(air.getHazevisibility().floatValue());
                resultList.add(cityinfo);
            }
        }
        return resultList;
    }

    @Override
    public Date initVisibilityForecastTime() {
        Date date = new Date();
        Map<String, String> selectParam = new HashMap<String, String>();
        RequestDataBySelect selectRequest = new RequestDataBySelect();
        String[] columns = new String[]{"to_char(datechar, 'yyyy-MM-dd HH24:MI:SS') as datechar"};
        selectRequest.setColumns(columns);
        selectRequest.setSort(new String[]{"datechar desc"});
        selectRequest.setLimit(1);
        try {
            selectParam.put("method", "hazevisibility_forecast");
            selectParam.put("sqlType", "select");
            selectParam.put("advCode", DbConfig.advCode);
            selectParam.put("param", JsonUtil.object2Json(selectRequest));
            String message =procon.processRequest(selectParam);
            date = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<hazevisibility_forecast>>() {
            }).get(0).getDatechar();
        } catch (Exception e) {
            // TODO: handle exception
        }
        return date;
    }

    @Override
    public Date initSmogTime() {
        Date date = new Date();
        Map<String, String> selectParam = new HashMap<String, String>();
        RequestDataBySelect selectRequest = new RequestDataBySelect();
        String[] columns = new String[]{"to_char(datechar, 'yyyy-MM-dd HH24:MI:SS') as datechar"};
        selectRequest.setColumns(columns);
        selectRequest.setSort(new String[]{"datechar desc"});
        selectRequest.setLimit(1);
        try {
            selectParam.put("method", "air_haze_warn");
            selectParam.put("sqlType", "select");
            selectParam.put("advCode", DbConfig.advCode);
            selectParam.put("param", JsonUtil.object2Json(selectRequest));
            String message =procon.processRequest(selectParam);
            date = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<air_haze_warn>>() {
            }).get(0).getDatechar();
        } catch (Exception e) {
            // TODO: handle exception
        }
        return date;
    }

    @Override
    public List<SmogFile> getSmogFile(String s1, String s2) {
         List<air_haze_warn> listData = new ArrayList<air_haze_warn>();
        List<SmogFile> resultList = new ArrayList<SmogFile>();
        Map<String, String> selectParam = new HashMap<String, String>();
        RequestDataBySelect selectRequest = new RequestDataBySelect();
        List<String> columns = new ArrayList<String>();
        columns.add("id");
        columns.add("to_char(datechar, 'yyyy-MM-dd HH24:MI:SS') as datechar");
        columns.add("filename");
//        columns.add("filepath");
        // 拼接where字段
        StringBuilder strWhere = new StringBuilder();
        int whereCount = 0;
        if (s1 != null) {
            strWhere.append(String.format(" and datechar>='%s'", s1));
        }
        if (s2 != null) {
            strWhere.append(String.format(" and datechar<='%s'", s2));
        }
        if (whereCount == 0 && !StringUtil.IsNullOrEmpty(strWhere.toString())) {
            strWhere = strWhere.replace(0, 4, "");
        }
        selectRequest.setParam(strWhere.toString());
        selectRequest.setColumns(columns.toArray(new String[columns.size()]));
        try {
            selectParam.put("method", "air_haze_warn");
            selectParam.put("sqlType", "select");
           selectParam.put("advCode", DbConfig.advCode);
            selectParam.put("param", JsonUtil.object2Json(selectRequest));
            String message = procon.processRequest(selectParam);
            listData = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<air_haze_warn>>() {
            });
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println("error!");
        }
        if (listData != null && listData.size() > 0) {
            for (air_haze_warn air : listData) {
                SmogFile cityinfo = new SmogFile();
                cityinfo.setID(air.getId().toString());
                cityinfo.setDateChar(air.getDatechar().toString());
                cityinfo.setFileName(air.getFilename());
                cityinfo.setFilePath(air.getFilepath());
                resultList.add(cityinfo);
            }
        }
        return resultList;
    }

    @Override
    public String getValueByName(String s) {
        List<sys_config> listData = new ArrayList<sys_config>();
        String resultList = "";
        Map<String, String> selectParam = new HashMap<String, String>();
        RequestDataBySelect selectRequest = new RequestDataBySelect();
        List<String> columns = new ArrayList<String>();
        columns.add("paramvalue");
        // 拼接where字段
        StringBuilder strWhere = new StringBuilder();
        int whereCount = 0;
        if (s != null) {
            strWhere.append(String.format(" and paramname='%s'", s));
        }
        if (whereCount == 0 && !StringUtil.IsNullOrEmpty(strWhere.toString())) {
            strWhere = strWhere.replace(0, 4, "");
        }
        selectRequest.setParam(strWhere.toString());
        selectRequest.setColumns(columns.toArray(new String[columns.size()]));
        try {
            selectParam.put("method", "sys_config");
            selectParam.put("sqlType", "select");
            selectParam.put("advCode", DbConfig.advCode);
            selectParam.put("param", JsonUtil.object2Json(selectRequest));
            String message =procon.processRequest(selectParam);
            listData = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<sys_config>>() {
            });
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println("error!");
        }
        if (listData != null && listData.size() > 0) {
            for (sys_config air : listData) {
                resultList = air.getParamvalue();
            }
        }
        return resultList;
    }

    @Override
    public Date airPolInitTime() {
        Date date = new Date();
        Map<String, String> selectParam = new HashMap<String, String>();
        RequestDataBySelect selectRequest = new RequestDataBySelect();
        String[] columns = new String[]{"to_char(observtime, 'yyyy-MM-dd HH24:MI:SS') as observtime"};
        selectRequest.setColumns(columns);
        selectRequest.setSort(new String[]{"observtime desc"});
        selectRequest.setLimit(1);
        try {
            selectParam.put("method", "airpol_con_fcst");
            selectParam.put("sqlType", "select");
            selectParam.put("advCode", DbConfig.advCode);
            selectParam.put("param", JsonUtil.object2Json(selectRequest));
            String message =procon.processRequest(selectParam);
            date = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<airpol_reg_fcst>>() {
            }).get(0).getObservtime();
        } catch (Exception e) {
            // TODO: handle exception
        }
        return date;
    }

    @Override
    public Date makeRefineInit() {
        Date date = new Date();
        Map<String, String> selectParam = new HashMap<String, String>();
        RequestDataBySelect selectRequest = new RequestDataBySelect();
        String[] columns = new String[]{"to_char(datechar, 'yyyy-MM-dd HH24:MI:SS') as datechar"};
        selectRequest.setColumns(columns);
        selectRequest.setSort(new String[]{"datechar desc"});
        selectRequest.setLimit(1);
        try {
            selectParam.put("method", "airpol_fcst");
            selectParam.put("sqlType", "select");
            selectParam.put("advCode", DbConfig.advCode);
            selectParam.put("param", JsonUtil.object2Json(selectRequest));
            String message = procon.processRequest(selectParam);
            date = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<airpol_fcst>>() {
            }).get(0).getDatechar();
        } catch (Exception e) {
            // TODO: handle exception
        }
        return date;
    }

    @Override
    public List<heavy_forecast> getRGresult(String date, String type) {
        // TODO 手动判断重污染天气
        List<heavy_forecast> listData = new ArrayList<heavy_forecast>();
        Map<String, String> selectParam = new HashMap<String, String>();
        RequestDataBySelect selectRequest = new RequestDataBySelect();
        // 拼接where字段
        StringBuilder strWhere = new StringBuilder();
        if (!StringUtil.IsNullOrEmpty(date)) {
            strWhere.append(String.format(" and time='%s'", date));
        }
        if (!StringUtil.IsNullOrEmpty(type)) {
            strWhere.append(String.format(" and judgetype='%s'", type));
        }
        if (!StringUtil.IsNullOrEmpty(strWhere.toString())) {
            strWhere = strWhere.replace(0, 4, "");
        }
        selectRequest.setParam(strWhere.toString());
        try {
            selectParam.put("method", "heavy_forecast");
            selectParam.put("sqlType", "select");
            selectParam.put("advCode", DbConfig.advCode);
            selectParam.put("param", JsonUtil.object2Json(selectRequest));
            String message =procon.processRequest(selectParam);
            listData = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<heavy_forecast>>() {
            });
        } catch (Exception e) {
            // TODO: handle exception
        }
        return listData;
    }

    @Override
    public Date getheavyFcst() {
        Date date = new Date();
        Map<String, String> selectParam = new HashMap<String, String>();
        RequestDataBySelect selectRequest = new RequestDataBySelect();
        String[] columns = new String[]{"to_char(time, 'yyyy-MM-dd HH24:MI:SS') as time"};
        selectRequest.setColumns(columns);
        selectRequest.setSort(new String[]{"time desc"});
        selectRequest.setLimit(1);
        try {
            selectParam.put("method", "heavy_forecast");
            selectParam.put("advCode", DbConfig.advCode);
            selectParam.put("sqlType", "select");
            selectParam.put("param", JsonUtil.object2Json(selectRequest));
            String message = procon.processRequest(selectParam);
            date = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<heavy_forecast>>() {
            }).get(0).getTime();
        } catch (Exception e) {
            // TODO: handle exception
        }
        return date;
    }

    @Override
    public Date airRegInitTime() {
        // TODO Auto-generated method stub
        Date date = new Date();
        Map<String, String> selectParam = new HashMap<String, String>();
        RequestDataBySelect selectRequest = new RequestDataBySelect();
        String[] columns = new String[]{"to_char(observtime, 'yyyy-MM-dd HH24:MI:SS') as observtime"};
        selectRequest.setColumns(columns);
        selectRequest.setSort(new String[]{"observtime desc"});
        selectRequest.setLimit(1);
        try {
            selectParam.put("method", "airpol_reg_fcst");
            selectParam.put("sqlType", "select");
            selectParam.put("advCode", DbConfig.advCode);
            selectParam.put("param", JsonUtil.object2Json(selectRequest));
            String message = procon.processRequest(selectParam);
            date = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<airpollution>>() {
            }).get(0).getObservtime();
        } catch (Exception e) {
            // TODO: handle exception
        }
        return date;
    }

    @Override
    public List<AirPolRegForecast> getZYAirPolRegForecast(String s2) {
        List<airpol_reg_fcst> listData = new ArrayList<airpol_reg_fcst>();
        List<AirPolRegForecast> resultList = new ArrayList<AirPolRegForecast>();
        Map<String, String> selectParam = new HashMap<String, String>();
        RequestDataBySelect selectRequest = new RequestDataBySelect();
        List<String> columns = new ArrayList<String>();
        columns.add(
                "to_char(cast(observtime as timestamp)+ cast((forecasttype)||'hour' as INTERVAL),'yyyy-MM-dd HH24:MI:SS') as observtime");
        columns.add("aqi");
        columns.add("forecasttype");
        columns.add("pm2p5");
        columns.add("pm10");
        columns.add("o3_1h");
        columns.add("o3_8h");
        selectRequest.setColumns(columns.toArray(new String[columns.size()]));
        // 拼接where字段
        StringBuilder strWhere = new StringBuilder();
        int whereCount = 0;
        if (s2 != null) {
            strWhere.append(String.format(" and observtime::DATE='%s'", s2));
        }
        if (whereCount == 0 && !StringUtil.IsNullOrEmpty(strWhere.toString())) {
            strWhere = strWhere.replace(0, 4, "");
        }
        selectRequest.setParam(strWhere.toString());
        try {
            selectParam.put("method", "airpol_reg_fcst");
            selectParam.put("sqlType", "select");
            selectParam.put("advCode", DbConfig.advCode);
            selectParam.put("param", JsonUtil.object2Json(selectRequest));
            String message = procon.processRequest(selectParam);
            listData = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<airpol_reg_fcst>>() {
            });
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println("error!!!!");
        }
        if (listData != null && listData.size() > 0) {
            for (airpol_reg_fcst airpol : listData) {
                AirPolRegForecast airpolregforecast = new AirPolRegForecast();
                Date date = airpol.getObservtime();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                airpolregforecast.setObservTime(sdf.format(date));
                airpolregforecast.setAQI(airpol.getAqi());
                airpolregforecast.setForecastType(airpol.getForecasttype());
                airpolregforecast.setPM2p5(airpol.getPm2p5());
                airpolregforecast.setPM10(airpol.getPm10());
                airpolregforecast.setO3_1H(airpol.getO3_1h());
                airpolregforecast.setO3_8H(airpol.getO3_8h());
                resultList.add(airpolregforecast);
            }
        }
        return resultList;
    }

    @Override
    public List<AirPolForecast> getAirPolForecast1(String s1, String s2) {
        List<airpol_fcst> listData = new ArrayList<airpol_fcst>();
        List<AirPolForecast> resultList = new ArrayList<AirPolForecast>();
        Map<String, String> selectParam = new HashMap<String, String>();
        RequestDataBySelect selectRequest = new RequestDataBySelect();
        List<String> columns = new ArrayList<String>();
        columns.add("stationid");
        columns.add("to_char(datechar, 'yyyy-MM-dd HH24:MI:SS') as datechar");
        columns.add("stationname");
        columns.add("longitude");
        columns.add("latitude");
        columns.add("pm10");
        columns.add("o3_1h");
        columns.add("o3_8h");
        columns.add("pm2_5");
        columns.add("so2");
        columns.add("no2");
        columns.add("co");
        columns.add("aqi");
        columns.add("pri");
        columns.add("grade");
        // 拼接where字段
        StringBuilder strWhere = new StringBuilder();
        int whereCount = 0;
        if (s1 != null) {
            strWhere.append(String.format(" and datechar>='%s'", s1));
        }
        if (s2 != null) {
            strWhere.append(String.format(" and forecasttype='%s'", s2));
        }
        if (whereCount == 0 && !StringUtil.IsNullOrEmpty(strWhere.toString())) {
            strWhere = strWhere.replace(0, 4, "");
        }
        selectRequest.setParam(strWhere.toString());
        selectRequest.setColumns(columns.toArray(new String[columns.size()]));
        try {
            selectParam.put("method", "airpol_fcst");
            selectParam.put("sqlType", "select");
            selectParam.put("advCode", DbConfig.advCode);
            selectParam.put("param", JsonUtil.object2Json(selectRequest));
            String message = procon.processRequest(selectParam);
            listData = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<airpol_fcst>>() {
            });
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println("error!");
        }
        if (listData != null && listData.size() > 0) {
            for (airpol_fcst air : listData) {
                AirPolForecast cityinfo = new AirPolForecast();
                cityinfo.setStationID(air.getStationid());
                cityinfo.setDateChar(air.getDatechar());
                cityinfo.setStationName(air.getStationname());
                cityinfo.setLongitude(air.getLongitude());
                cityinfo.setLatitude(air.getLatitude());
                cityinfo.setPM2_5(air.getPm25());
                cityinfo.setPM10(air.getPm10());
                cityinfo.setSO2(air.getSo2());
                cityinfo.setNO2(air.getNo2());
                cityinfo.setCO(air.getCo());
                cityinfo.setAQI(air.getAqi());
                cityinfo.setPRI(air.getPri());
                cityinfo.setGRADE(air.getGrade());
                resultList.add(cityinfo);
            }
        }
        return resultList;
    }

    @Override
    public Object initStations() {
        List<township_station> listData = new ArrayList<township_station>();
        Map<String, String> selectParam = new HashMap<String, String>();
        RequestDataBySelect selectRequest = new RequestDataBySelect();
        List<String> columns = new ArrayList<String>();
        columns.add("stationid");
        columns.add("stationname");
        selectRequest.setColumns(columns.toArray(new String[columns.size()]));
        selectRequest.setParam("stationid like '5%' and city like '"+DbConfig.CITY+"%'");
        try {
            selectParam.put("method", "township_station");
            selectParam.put("sqlType", "select");
            selectParam.put("param", JsonUtil.object2Json(selectRequest));
            String message = procon.processRequest(selectParam);
            listData = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<township_station>>() {
            });
        } catch (Exception e) {
            // TODO: handle exception
        }
        return listData;
    }
}
